package com.prs.crm.domain.stock;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

import com.prs.crm.domain.BaseEntity;
import com.prs.crm.domain.purchase.PurchaseSupplierInquiryDetail;

/**
 * 采购明细
 * 
 * @author prs.chenzhi
 * 
 */
@Entity
@Table(name = "stock_purchasedetail")
public class PurchaseDetail extends BaseEntity {

	private static final long serialVersionUID = 2625159668537269733L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@ManyToOne
	private Product product;

	@ManyToOne
	private Purchase purchase;

	/**
	 * 采购数量
	 */
	private int stockNum;

	/**
	 * 采购的实际单价
	 */
	private Double price;

	/**
	 * 折扣
	 */
	private Double discount;

	private Double total;

	/**
	 * 已入库数量
	 */
	private Integer storedNum;

	@OneToMany(mappedBy = "purchasedetail")
	@OrderBy
	@Cascade( { org.hibernate.annotations.CascadeType.ALL,
			org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	private Set<PurchaseSupplierInquiryDetail> inquirydetail;

	@ManyToOne
	private Supplier supplier;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}

	public void setStockNum(int stockNum) {
		this.stockNum = stockNum;
	}

	public int getStockNum() {
		return stockNum;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getPrice() {
		return price;
	}

	public void setDiscount(Double discount) {
		this.discount = discount;
	}

	public Double getDiscount() {
		return discount;
	}

	public void setTotal(Double total) {
		this.total = total;
	}

	public Double getTotal() {
		return total;
	}

	public void setPurchase(Purchase purchase) {
		this.purchase = purchase;
	}

	public Purchase getPurchase() {
		return purchase;
	}

	public void setStoredNum(Integer storedNum) {
		this.storedNum = storedNum;
	}

	public Integer getStoredNum() {
		return storedNum;
	}

	/**
	 * 获取最大可入库数量
	 * 
	 * @return
	 */
	public int getMaxStoreNum() {
		if (storedNum != null) {
			return (stockNum - storedNum);
		} else {
			return this.stockNum;
		}
	}

	public Set<PurchaseSupplierInquiryDetail> getInquirydetail() {
		return inquirydetail;
	}

	public void setInquirydetail(
			Set<PurchaseSupplierInquiryDetail> inquirydetail) {
		this.inquirydetail = inquirydetail;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

}
